////////////////////////////////////////////////////////////////////////////////
// Copyright 2005 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   TASK
// AUTHOR           :   Ric Yik
// DATE CREATE      :   2005/12/29
// DESCRIPTION      :   a MAINTENANCE FORM file for the function, TASK
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2005/12/29   Ric Yik         1.0                 source is generated.
// 2006/08/28   Jimmy Chen      1.3                 fix the bug: Task Code should not allow invalide data
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.taskMaster.web;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.util.ProjectCommonUtil;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.form.SimpleMaintStyleFormTemplate;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

/**
* <p>A <strong>MaintenanceForm</strong> is a JavaBean which provides property
* getter and setter methods for all of the bean properties, and implements
* <code>Serializable</code>, as required by the JavaBean specification
* The main purpose for this bean is to get the user input
* from the web browsers.
*
* @author          Ric Yik
* @version 1.0     2005/12/29
* @since           FrameWork 1.0
*
*/
public class TaskMaintenanceForm extends SimpleMaintStyleFormTemplate {

    //Header Attributes (DB Table Name: T_TASK_HDR)
    private String companyCode;                      //DB Column Name: COMPANY_CODE
    private String docType;                          //DB Column Name: DOC_TYPE
    private String docDesc;                          //DB Column Name: DOC_DESC
    private String status;                           //DB Column Name: STATUS
    private String dcInd;                            //DB Column Name: DC_IND
    private String dcDate;                           //DB Column Name: DC_DATE
    private String createUser;                       //DB Column Name: CREATE_USER
    private String createDate;                       //DB Column Name: CREATE_DATE
    private String updateUser;                       //DB Column Name: UPDATE_USER
    private String updateDate;                       //DB Column Name: UPDATE_DATE

    //Child Attributes (DB Table Name: T_TASK_DTL)
    private String[] taskCode;                       //DB Column Name: TASK_CODE
    private String[] dtlTaskDesc;                    //DB Column Name: TASK_DESC
    private String[] dtlUserGroup;                   //DB Column Name: USER_GROUP
    private String[] dtlUserCode;                    //DB Column Name: USER_CODE
    private String[] dtlDateType;                    //DB Column Name: DATE_TYPE
    private String[] dtlNoDateFrom;                  //DB Column Name: NO_DATE_FROM
    private String[] dtlDuration;                    //DB Column Name: DURATION
    private String[] dtlStatus;                      //DB Column Name: STATUS
    private String[] dtlIsDefault;                   //DB Column Name: IS_DEFAULT
    private String[] dtlDcInd;                       //DB Column Name: DC_IND
    private String[] dtlDcDate;                      //DB Column Name: DC_DATE
    private String[] dtlSeq;                          //DB Column Name: SEQ;

    /**
    * Validate the properties that have been set for this non-HTTP request,
    * and return an <code>ActionErrors</code> object that encapsulates any
    * validation errors that have been found.  If no errors are found, return
    * <code>null</code> or an <code>ActionErrors</code> object with no
    * recorded error messages.
    * <p>
    * The default implementation attempts to forward to the HTTP version of
    * this method.
    *
    * @param      the action errors array
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public void otherValidate(ActionErrors errors) {
        validateHdr(errors);
        validateDtl(errors);
    }

    /**
    * Validate the properties that have been set for this non-HTTP request,
    * and return an <code>ActionErrors</code> object that encapsulates any
    * validation errors that have been found.  If no errors are found, return
    * <code>null</code> or an <code>ActionErrors</code> object with no
    * recorded error messages.
    * <p>
    * The default implementation attempts to forward to the HTTP version of
    * this method.
    *
    * @param      errors The errors array that is used for storing validation errors
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public void validateHdr(ActionErrors errors) {
        if (CommonUtil.isEmpty(this.getCompanyCode())) {
            ActionErrorUtil.addErrorForGeneralFieldBlank(errors, "Company Code");
        }
        if (CommonUtil.isEmpty(this.getDocType())) {
            ActionErrorUtil.addErrorForGeneralFieldBlank(errors, "Type Code");
        }
        if (CommonUtil.isEmpty(this.getDocDesc())) {
            ActionErrorUtil.addErrorForGeneralFieldBlank(errors, "Document");
        }
    }

    /**
    * Validate the properties that have been set for this non-HTTP request,
    * and return an <code>ActionErrors</code> object that encapsulates any
    * validation errors that have been found.  If no errors are found, return
    * <code>null</code> or an <code>ActionErrors</code> object with no
    * recorded error messages.
    * <p>
    * The default implementation attempts to forward to the HTTP version of
    * this method.
    *
    * @param      errors The errors array that is used for storing validation errors
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public void validateDtl(ActionErrors errors) {

        for (Iterator rows = this.getMultiRows(0).iterator(); rows.hasNext();) {
            TaskDtlRow row = (TaskDtlRow) rows.next();
            if (row == null || !CommonUtil.isEmpty(row.getIsDelete())) {
                continue;
            }

            String fieldDescription;

            fieldDescription = "Task Code";
            if (CommonUtil.isEmpty(row.getTaskCode())) {
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, fieldDescription);
                // If Task Code is blank, then the subsequent validation errors
                // will have no line reference and therefore meaningless.
                break;
            }
            else
            {
                WebUtil.validateCode(row.getTaskCode(), "Task Code", errors);
            }

            fieldDescription = "Date Type - {" + row.getTaskCode() + "}";
            if (CommonUtil.isEmpty(row.getDtlDateType())) {
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, fieldDescription);
            }

            fieldDescription = "Duration - {" + row.getTaskCode() + "}";
            if (!CommonUtil.isEmpty(row.getDtlDuration())) {
                if (!CommonUtil.isValidNumber(row.getDtlDuration(), 5, 0)) {
                    ActionErrorUtil.addErrorForInvalidNumber(errors, fieldDescription);
                } else if (!CommonUtil.isValidPositiveIntegerOrZero(row.getDtlDuration())) {
                    ActionErrorUtil.addErrorForInvalidNegativeInteger(errors, fieldDescription);
                }
            }

            fieldDescription = "Default No. of Date from Date Type - {" + row.getTaskCode() + "}";
            if (CommonUtil.isEmpty(row.getDtlNoDateFrom())) {
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, fieldDescription);
            } else if (!CommonUtil.isValidNumber(row.getDtlNoDateFrom(), 5, 0)) {
                ActionErrorUtil.addErrorForInvalidNumber(errors, fieldDescription);
            }

        }
        int i=0;
        HashMap maps=new HashMap();
        for (Iterator rows = this.getMultiRows(0).iterator(); rows.hasNext();) {
            TaskDtlRow row = (TaskDtlRow) rows.next();
            if (row == null || !CommonUtil.isEmpty(row.getIsDelete())) {
                continue;
            }
           if(!maps.containsValue(row.getTaskCode())){
               maps.put(String.valueOf(i++),row.getTaskCode());
           } else {
               ActionErrorUtil.addErrorForSpecficPurpose(errors,"[Detail] No duplicate Task Code allowed.");
           }
        }
    }

    /**
    * Validate the properties that have been set for this non-HTTP request,
    * and return an <code>ActionErrors</code> object that encapsulates any
    * validation errors that have been found.  If no errors are found, return
    * <code>null</code> or an <code>ActionErrors</code> object with no
    * recorded error messages.
    * <p>
    * The default implementation attempts to forward to the HTTP version of
    * this method.
    *
    * @param      mapping      the action mapping
    * @param      formIn       the action form
    * @param      request      the http request
    * @param      response     the http response
    * @param      errors       the action errors array
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public void moreValidate(ActionMapping mapping, ActionForm formIn,
        HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        TaskMaintenanceForm form = (TaskMaintenanceForm) formIn;

        try {

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
    }

    /**
    * Reset all bean properties to their default state.  This method
    * is called inside the reset() method.
    *
    * @param      mapping The mapping used to select this instance
    * @param      request The servlet request we are processing
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public void otherReset(ActionMapping mapping, HttpServletRequest request) {

    }

    /**
    * Converts to a string representing the data in this object.
    * A new <code>String</code> object is allocated and initialized to
    * contain the character sequence currently represented by this
    * object. This <code>String</code> is then returned. Subsequent
    * changes to this object do not affect the contents of the
    * <code>String</code>.
    *
    * @return     a string representation of the object.
    * @see        java.lang.Object#toString()
    * @author     Ric Yik
    * @since      FrameWork 1.0
    */
    public String toString() {
        String pkStr = "PK: {companyCode = " + this.companyCode
            + "\n, docType = " + this.docType + "}\n";
        String colStr = "COL: {docDesc = " + this.docDesc
            + "\n, status = " + this.status
            + "\n, dcInd = " + this.dcInd
            + "\n, dcDate = " + this.dcDate
            + "\n, createUser = " + this.createUser
            + "\n, createDate = " + this.createDate
            + "\n, updateUser = " + this.updateUser
            + "\n, updateDate = " + this.updateDate + "}\n";
        return "TaskMaintenanceForm.toString() (header only) is:\n" + pkStr + colStr;
    }

    ////////////////////////////////////////////////////////////////////////////////
    //Generated Accessors (get/set methods) for Header (TaskHdr)
    ////////////////////////////////////////////////////////////////////////////////

    public String getCompanyCode() {
        return this.companyCode;
    }

    public String getDocType() {
        return this.docType;
    }

    public String getDocDesc() {
        return this.docDesc;
    }

    public String getStatus() {
        return this.status;
    }

    public String getDcInd() {
        return this.dcInd;
    }

    public String getDcDate() {
        return this.dcDate;
    }

    public String getCreateUser() {
        return this.createUser;
    }

    public String getCreateDate() {
        return this.createDate;
    }

    public String getUpdateUser() {
        return this.updateUser;
    }

    public String getUpdateDate() {
        return this.updateDate;
    }

    public void setCompanyCode(String companyCode) {
        this.companyCode = ProjectCommonUtil.toCode(companyCode);
    }

    public void setDocType(String docType) {
        this.docType = ProjectCommonUtil.toCode(docType);
    }

    public void setDocDesc(String docDesc) {
        this.docDesc = docDesc;
    }

    public void setStatus(String status) {
        this.status = ProjectCommonUtil.toCode(status);
    }

    public void setDcInd(String dcInd) {
        this.dcInd = dcInd;
    }

    public void setDcDate(String dcDate) {
        this.dcDate = dcDate;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }

    public void setUpdateDate(String updateDate) {
        this.updateDate = updateDate;
    }

    ////////////////////////////////////////////////////////////////////////////////
    //Generated Accessors (get/set methods) for Child (TaskDtl)
    ////////////////////////////////////////////////////////////////////////////////

    public String[] getTaskCode() {
        return this.taskCode;
    }

    public String[] getDtlTaskDesc() {
        return this.dtlTaskDesc;
    }

    public String[] getDtlUserGroup() {
        return this.dtlUserGroup;
    }

    public String[] getDtlUserCode() {
        return this.dtlUserCode;
    }

    public String[] getDtlDateType() {
        return this.dtlDateType;
    }

    public String[] getDtlNoDateFrom() {
        return this.dtlNoDateFrom;
    }

    public String[] getDtlDuration() {
        return this.dtlDuration;
    }

    public String[] getDtlStatus() {
        return this.dtlStatus;
    }

    public String[] getDtlIsDefault() {
        return this.dtlIsDefault;
    }

    public String[] getDtlDcInd() {
        return this.dtlDcInd;
    }

    public String[] getDtlDcDate() {
        return this.dtlDcDate;
    }

    public void setTaskCode(String[] taskCode) {
        this.taskCode = taskCode;
    }

    public void setDtlTaskDesc(String[] dtlTaskDesc) {
        this.dtlTaskDesc = dtlTaskDesc;
    }

    public void setDtlUserGroup(String[] dtlUserGroup) {
        this.dtlUserGroup = dtlUserGroup;
    }

    public void setDtlUserCode(String[] dtlUserCode) {
        this.dtlUserCode = dtlUserCode;
    }

    public void setDtlDateType(String[] dtlDateType) {
        this.dtlDateType = dtlDateType;
    }

    public void setDtlNoDateFrom(String[] dtlNoDateFrom) {
        this.dtlNoDateFrom = dtlNoDateFrom;
    }

    public void setDtlDuration(String[] dtlDuration) {
        this.dtlDuration = dtlDuration;
    }

    public void setDtlStatus(String[] dtlStatus) {
        this.dtlStatus = dtlStatus;
    }

    public void setDtlIsDefault(String[] dtlIsDefault) {
        this.dtlIsDefault = dtlIsDefault;
    }

    public void setDtlDcInd(String[] dtlDcInd) {
        this.dtlDcInd = dtlDcInd;
    }

    public void setDtlDcDate(String[] dtlDcDate) {
        this.dtlDcDate = dtlDcDate;
    }

    ////////////////////////////////////////////////////////////////////////////////
    //Other Accessors (get/set methods)
    ////////////////////////////////////////////////////////////////////////////////

    public int getPageEventSrcIdx() {
        return Integer.parseInt(this.getPageEventSrc());
    }
    public Iterator getCreateUserValues() {
        return this.getTemplate().getUserNameValues(this, "createUser");
    }

    public Iterator getUpdateUserValues() {
        return this.getTemplate().getUserNameValues(this, "updateUser");
    }

    public String[] getDtlSeq() {
        return dtlSeq;
    }

    public void setDtlSeq(String[] dtlSeq) {
        this.dtlSeq = dtlSeq;
    }
}